package com.ebt.m.customer.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.ebt.m.data.bean.BeanCustomerTape;

import java.io.File;

/**
 * Created by Rick.Wang on 2016/6/13.
 */
public class TapeShowButton extends TextView implements View.OnClickListener{

    private BeanCustomerTape mTape;
    private RecorderWrapper mRecorderWrapper;

    public TapeShowButton(Context context) {
        super(context);
        init();
    }

    public TapeShowButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TapeShowButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        setOnClickListener(this);
    }

    public void update(BeanCustomerTape tape, RecorderWrapper recorderWrapper){
        mTape = tape;
        mRecorderWrapper = recorderWrapper;
    }

    @Override
    public void onClick(View v) {
        if(new File(mTape.getFilePath()).exists()) {
            boolean isPlaying = false;
            try {
                isPlaying = mRecorderWrapper.isPlaying();
            }
            catch (IllegalStateException e) {
                e.printStackTrace();
                mRecorderWrapper = null;
                mRecorderWrapper = RecorderWrapper.getInstance();
            }
            if(isPlaying){
                mRecorderWrapper.pausePlayRecordFile();
            }else {
                mRecorderWrapper.playRecordFile(mTape.getFilePath());
            }
        }
    }
}
